package cn.legend.travel.admin.destination.dao.persist.repository.impl;

import cn.legend.travel.admin.destination.dao.persist.mapper.SceneryMapper;
import cn.legend.travel.admin.destination.dao.persist.repository.ISceneryRepository;
import cn.legend.travel.admin.destination.pojo.entity.Scenery;
import cn.legend.travel.admin.destination.pojo.param.SceneryAddNewParam;
import cn.legend.travel.admin.destination.pojo.param.SceneryUpdateParam;
import cn.legend.travel.admin.destination.pojo.vo.SceneryAdminVO;
import cn.legend.travel.common.enumerator.ServiceCode;
import cn.legend.travel.common.exception.ServiceException;
import cn.legend.travel.common.pojo.vo.PageData;
import cn.legend.travel.common.util.PageInfoToPageDataConverter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

@Repository
@RequiredArgsConstructor
public class SceneryRepositoryImpl implements ISceneryRepository {
    private final SceneryMapper mapper;

    @Override
    public PageData<SceneryAdminVO> list(int pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return PageInfoToPageDataConverter.convert(new PageInfo<>(mapper.list()));
    }

    @Override
    public int update(SceneryUpdateParam param) {
        return mapper.updateById(new Scenery(param));
    }

    @Override
    public int delete(Long id) {
        return mapper.deleteById(id);
    }

    @Override
    public Long addNew(SceneryAddNewParam param) {
        Scenery scenery = new Scenery();
        BeanUtils.copyProperties(param, scenery);
        int row = mapper.create(scenery);
        if (row != 1) {
            throw new ServiceException(ServiceCode.ERROR_INSERT, "服务器忙,请稍后再试.");
        }
        return scenery.getId();
    }

}
